﻿using System;
using System.Activities;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Umbrella.Whs.Service;

public static class CodeActivityContextExtensionMethods
{
    public static void SetWorkflowStatus(this CodeActivityContext ctx, string msg)
    {
        if (ctx == null)
            throw new NullReferenceException("this is null");

        if (string.IsNullOrEmpty(msg))
            throw new ArgumentException("null or empty", "msg");

        // get workflow manager
        var wm = ctx.GetExtension<WorkflowStatusManager>();

        if (wm == null)
            throw new Exception("WorkflowStatusManager is not an extension available in this workflow;");

        wm.SetStatus(ctx.WorkflowInstanceId, msg);
    }

    public static void SetWorkflowStatus(this CodeActivityContext ctx, string msg, float progress)
    {
        if (ctx == null)
            throw new NullReferenceException("this is null");

        if (string.IsNullOrEmpty(msg))
            throw new ArgumentException("null or empty", "msg");

        // get workflow manager
        var wm = ctx.GetExtension<WorkflowStatusManager>();

        if (wm == null)
            throw new Exception("WorkflowStatusManager is not an extension available in this workflow; add WorkflowStatusManager.Instance as extension when creating the workflow!");

        wm.SetStatus(ctx.WorkflowInstanceId, msg, progress);
    }

    public static void ClearWorkflowStatus(this CodeActivityContext ctx)
    {
        if (ctx == null)
            throw new NullReferenceException("this is null");

        // get workflow manager
        var wm = ctx.GetExtension<WorkflowStatusManager>();

        if (wm == null)
            throw new Exception("WorkflowStatusManager is not an extension available in this workflow; add WorkflowStatusManager.Instance as extension when creating the workflow!");

        wm.ClearStatus(ctx.WorkflowInstanceId);
    }
}

